package my.mark.mybaibaoxiang.twotiger.contract.utils;

import com.twotiger.util.FileUtil;
import com.twotiger.util.PathUtil;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 勾画个人方形章
 * @author TT_WTF
 * @date 2017年12月28日 上午10:31:08
 */
public class DrawSealUtils {
    private static final Logger LOG = Logger.getLogger(DrawSealUtils.class);

    private final static int ReatWIDTH = 70;//方章的宽度

    private final static int ReatHEIGHT = 35;//方章的高度

    /**
     * 画方形章方法
     * 参数就是章上面的名字。
     */
    public static String drawRectChapter(String name) {
        ByteArrayOutputStream out = null;
        InputStream in = null;
        // 创建缓存
        BufferedImage bufImg = new BufferedImage(ReatWIDTH, ReatHEIGHT, BufferedImage.TYPE_INT_RGB);
        // 获得画布
        Graphics2D gs = bufImg.createGraphics();
        // 设置颜色
        gs.setColor(Color.WHITE);
        // 填充区域
        gs.fillRect(0, 0, ReatWIDTH, ReatHEIGHT);
        // 设置画笔颜色
        gs.setColor(new Color(204, 41, 41));
        // 设置画笔宽度
        gs.setStroke(new BasicStroke(3));
        // 画方形边框
        gs.drawRect(2, 2, ReatWIDTH - 4, ReatHEIGHT - 4);
        // 设置字体
        gs.setFont(new Font("宋体", Font.BOLD, 15));
        // 画数据(合同专用章)
        LOG.info("制作方形章 :" + name);
        //gs.drawString(name, 28, 23);
        drawCenterMessage(gs, name, ReatWIDTH / 2.5, ReatHEIGHT / 1.5);
        gs.dispose();
        try {
            out = new ByteArrayOutputStream();
            ImageIO.write(bufImg, "PNG", out);
            InputStream is = new ByteArrayInputStream(out.toByteArray());
            String relativePath = PathUtil.generateRelativePath("src\\main\\java\\my\\mark\\mybaibaoxiang\\twotiger\\contract", name + ".png");
            writeToLocal(relativePath,is);
            /*boolean ok = FileStoreageFactory.getDefaultContract().save(is, ROOT + relativePath);
            if(!ok){
                relativePath = "";
            }*/
            return relativePath;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 化中心横字
     * @param gs
     * @param message
     * @param CENTERX
     * @param CENTERY
     */
    public static void drawCenterMessage(Graphics2D gs, String message, double CENTERX,
                                         double CENTERY) {
        if (message != null) {
            // 根据输入字符串得到字符数组
            String[] messages2 = message.split("", 0);
            String[] messages = new String[messages2.length];
            System.arraycopy(messages2, 0, messages, 0, messages2.length);
            // 输入的字数
            int ilength = messages.length;
            Font f = gs.getFont();
            
            FontRenderContext context = gs.getFontRenderContext();
            Rectangle2D bounds = f.getStringBounds(message, context);
            // 字符宽度＝字符串长度/字符数
            //double char_interval = (bounds.getWidth() / ilength) - 1;
            double char_interval = (1.2 * bounds.getWidth() / ilength) - 1;
            int first = 0, second = 0;
            boolean odd = false;
            if (ilength % 2 == 1) {
                first = (ilength - 1) / 2;
                odd = true;
            } else {
                first = (ilength) / 2 - 1;
                second = (ilength) / 2;
                odd = false;
            }
            if (odd) {
                gs.drawString(messages[first],
                    (float) (CENTERX - char_interval / 2 - char_interval / 10), (float) CENTERY);
                // 中心点的右边
                for (int i = first + 1; i < ilength; i++) {
                    double x = CENTERX + (i - first - 0.6) * char_interval;
                    gs.drawString(messages[i], (float) x, (float) CENTERY);
                }
                // 中心点的左边
                for (int i = first - 1; i > -1; i--) {
                    double x = CENTERX - (first - i + 0.6) * char_interval;
                    gs.drawString(messages[i], (float) x, (float) CENTERY);
                }
            } else {
                // 中心点的右边
                for (int i = second; i < ilength; i++) {
                    double x = CENTERX + (i - second - 0.1) * char_interval;
                    gs.drawString(messages[i], (float) x, (float) CENTERY);
                }
                // 中心点的左边
                for (int i = first; i > -1; i--) {
                    double x = CENTERX - (first + 1 - i + 0.1) * char_interval;
                    gs.drawString(messages[i], (float) x, (float) CENTERY);
                }

            }
        }
    }

    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录
     * @param input    输入流
     * @throws IOException
     */
    public static void writeToLocal(String destination, InputStream input)
            throws IOException {
        File dir = new File(destination.substring(0,destination.lastIndexOf(File.separator)));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        input.close();
    }

    public static void main(String[] args) {
        String tmp = FileUtil.MD5FileName("123_sdfsdf_ssssssss.jpg");
        System.out.println(tmp);

        String relativePath = DrawSealUtils.drawRectChapter(" 王牧野");
        System.out.println(relativePath);

    }


}
